import CombatComponent from "../../Combats/CombatComponent";
import GameEvent from "../../Commons/GameEvent";
import { SkillIdCenter } from "../../Enums/GameEnum";
import SaveManager from "../../Managers/SaveManager";
import CombatUI from "../../UIs/UIs/CombatUI";
import SkillManager from "../Skills/SkillManager";
import GateTemplate from "./GateTemplate";
import WaveTemplate from "./WaveTemplate";

export enum GateEvent {
    GATEUIUPDATA_EVENT = "gateUIUpdata_event",
    RESETGATE_EVENT = "resetGate_event"
}
export default class GateManager implements IModelManager {

    private static _single: GateManager = null;
    private gateMap: Map<number, GateTemplate>;
    private waveMap: Map<number, WaveTemplate>;

    public currentWave: number = 0;
    public enemyNum: number = 0;
    private _challengeTime: number = 0;

    public get challengeTime(): number {
        return this._challengeTime;
    }
    public set challengeTime(value: number) {
        var num = value;
        if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1007)) {
            var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1007);
            num *= (1 + ey.valueNum * 0.01);
        }
        this._challengeTime = Math.ceil(num);
    }
    updataChallengeTime(dt: number) {
        this._challengeTime -= dt;
    }

    public static get single(): GateManager {
        if (GateManager._single == null) {
            GateManager._single = new GateManager();
        }
        return GateManager._single;
    }

    initConfig(): void {
        this.gateMap = new Map();
        var gateJson = cc.resources.get("configs/gate", cc.JsonAsset);
        for (const iterator of gateJson.json) {
            var gateTp = new GateTemplate(iterator);
            this.gateMap.set(gateTp.id, gateTp);
        }

        this.waveMap = new Map();
        var waveJson = cc.resources.get("configs/wave", cc.JsonAsset);
        for (const iterator of waveJson.json) {
            var waveTp = new WaveTemplate(iterator);
            this.waveMap.set(waveTp.id, waveTp);
        }
    }

    getGateTemplate(id: number): GateTemplate {
        return this.gateMap.get(id);
    }

    getWaveTemplate(id: number): WaveTemplate {
        return this.waveMap.get(id);
    }


    public resetGate() {
        CombatComponent.startCombat = false;
        this.currentWave = 0;
        var userdata = SaveManager.single.userdata;
        userdata.gateEntity.currentGateId = 1;
        SaveManager.single.saveData();
        GameEvent.eventNode.emit(GateEvent.RESETGATE_EVENT);
    }



}